//
//  NKDoctorDetailsViewController.m
//  NewKY
//
//  Created by Dee on 2016/11/15.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKDoctorDetailsViewController.h"
#import "NKDetailsTableViewCell.h"
#import <MJRefresh.h>
#import "NKDetailsModel.h"
#import <MJRefresh.h>
#import <SVProgressHUD.h>


#define NKDetailsCellID @"NKDetailsCellID"

@interface NKDoctorDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>

/*---- 接收模型数据 -----*/
@property(nonatomic,copy)NSMutableArray *modelArr;

/*---- tableView -----*/
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation NKDoctorDetailsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self postMethodGainData];

    [self setupUI];
}

 /*---- 网络请求数据 -----*/
-(void)postMethodGainData{
    
     [NetWorkingManager sendPOSTDataWithPath:@"http://iosapi.itcast.cn/doctor/matchDoctors.json.php" withParamters:@{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2} withProgress:^(float progress) {
         
     } success:^(BOOL isSuccess, id responseObject) {
        
         if (isSuccess) {
             NSLog(@"%@",responseObject);
            
            NSArray *dictArr = responseObject[@"data"];
             
            NSArray *modelArr = [NSArray modelArrayWithClass:[NKDetailsModel class] json:dictArr];
             
             NSLog(@"modelArr = %@", modelArr);

//             self.modelArr = [modelArr copy];
             [self.modelArr addObjectsFromArray:modelArr];
             
               NSLog(@"modelArr = %@", self.modelArr);
        
             [self.tableView reloadData];
         }
 
     } failure:^(NSError *error) {
         NSLog(@"%@",error);
     }];
}

-(void)setupUI{
    UITableView *tab = [[UITableView alloc]initWithFrame:self.view.bounds];
    tab.dataSource  = self;
    tab.delegate = self;
    tab.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:tab];
    self.tableView = tab;
    
    [tab registerNib:[UINib nibWithNibName:@"NKDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:NKDetailsCellID];
    
    //================== 上下拉刷新 =================//
    tab.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(netRequest)];
    tab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(netRequest)];
    //设置弹框的属性
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
}


- (void)netRequest{
    
    //根据不同的类型选择不同的接口
    NSString *URLStr = @"";
    NSDictionary *parameters = [[NSDictionary alloc] init];
    
    URLStr = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
    
    parameters = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};
    
    [SVProgressHUD setMinimumDismissTimeInterval:1.0];
    //显示遮罩层
    [SVProgressHUD setInfoImage:[UIImage imageNamed:@"0"]];
    [SVProgressHUD showInfoWithStatus:@"正在加载医生列表..."];
    
    [NetWorkingManager sendPOSTDataWithPath:URLStr withParamters:parameters withProgress:^(float progress) {
        
    } success:^(BOOL isSuccess, id responseObject) {
    
        if ([responseObject isKindOfClass:[NSError class]]) {
            [SVProgressHUD showErrorWithStatus:@"您的网络不给力呀~"];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            return ;
        }
        
        NSArray *dictArr = responseObject[@"data"];
        
        NSArray *modelArr = [NSArray modelArrayWithClass:[NKDetailsModel class] json:dictArr];
    
         [self.modelArr addObjectsFromArray:modelArr];
    
        //刷新数组
        [self.tableView reloadData];
        [SVProgressHUD showSuccessWithStatus:@"加载成功"];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        if (error != nil) {
            NSLog(@"%@",error);
        }
    }];
}


#pragma mark 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.modelArr.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NKDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NKDetailsCellID forIndexPath:indexPath];

    NKDetailsModel *model = self.modelArr[indexPath.row];

    cell.detailsModel = model;
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 90;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


-(NSMutableArray *)modelArr{
    if(_modelArr == nil){
        _modelArr = [NSMutableArray array];
    }
    return _modelArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
